package org.jeecg.modules.charge.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.StringUtils;
import org.jeecg.entity.charge.ChargeBills;
import org.jeecg.modules.charge.mapper.ChargeAppMapper;
import org.jeecg.modules.charge.mapper.ChargeBillsMapper;
import org.jeecg.modules.charge.entity.ChargeBillsOrder;
import org.jeecg.modules.charge.entity.ChargeType;
import org.jeecg.modules.charge.service.IChargeAppService;
import org.jeecg.modules.charge.service.IChargeTypeService;
import org.jeecg.modules.charge.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
* @Description: 收费APP调用接口
* @Author: Lee
* @Date:   2019-08-23
* @Version: V1.0
*/
@Slf4j
@Api(tags="app调用接口")
@RestController
@RequestMapping("/b/charge/app")
public class ChargeAppController {
   @Autowired
   private IChargeTypeService   chargeTypeService;
    @Autowired
    private IChargeAppService chargeAppService;
    @Autowired
    private ChargeBillsMapper chargeBillsMapper;
    @Autowired
    private ChargeAppMapper chargeAppMapper;
   /**
    * 查询对应的缴费类型
    *
    *
    */
   @AutoLog(value = "获取所有的缴费类型")
   @ApiOperation(value="获取所有的缴费类型", notes="获取所有的缴费类型")
   @PostMapping(value = "/queryTypes")
   public Result<List<ChargeType>> getTypes() {
      List<ChargeType> types=null;
      try{
          types=chargeTypeService.getTypes();
          return  Result.ok(types);
      }catch (Exception e ){
         e.printStackTrace();
         return Result.error("内部异常");
      }
   }
   /**
    * 查询对应的缴费类型下的对应账单
    *
    *
    */
   @AutoLog(value = "获取对应的缴费类型下账单")
   @ApiOperation(value="获取对应的缴费类型下账单", notes="获取对应的缴费类型下账单")
   @PostMapping(value = "/getBillsByType")
   public Result<List<ChargeBills>> getBillsByType(String roomId, String type,String cusOrRent,HttpServletRequest request) {
       List<ChargeBills> bills=null;
       String tentant = chargeAppMapper.getTenentByRoomId(roomId);
       request.setAttribute("tenantId",tentant);
      try{
         bills=chargeAppService.getBillsByRoomId(roomId,type,cusOrRent);
         return Result.ok(bills);
      }catch (Exception e ){
         e.printStackTrace();
         return Result.error("内部异常");
      }
   }

   /**
    * 查询对应的缴费类型下的对应账单
    *
    *
    */
   @AutoLog(value = "获取房间对应最后一次缴费记录")
   @ApiOperation(value="获取房间对应最后一次缴费记录", notes="获取房间对应最后一次缴费记录")
   @PostMapping(value = "/getLastPayRecords")
   public Result<ChargePayRecordsByRoom> getLastPayRecords(String roomId) {
      ChargePayRecordsByRoom records=null;
      try{
         records=chargeAppService.getPayRecordsByRoom(roomId);
         return Result.ok(records);
      }catch (Exception e ){
         e.printStackTrace();
         return Result.error("内部异常");
      }
   }

   /**
    * 查询对应的缴费类型下的对应账单
    *
    *
    */
   @AutoLog(value = "查询用户所有的缴费记录")
   @ApiOperation(value="查询用户所有的缴费记录", notes="查询用户所有的缴费记录")
   @PostMapping(value = "/getUserPayRecords")
   public Result<Map<String,List<ChargeBillsOrder>>> getUserPayRecords(String userId,HttpServletRequest request) {
      List<ChargeBillsOrder> orders=null;
      try{
        orders=chargeAppService.getPayRecordsByUid(userId);
        for(ChargeBillsOrder billsOrder:orders){
             request.setAttribute("tenantId",billsOrder.getTenantId());
            if(StringUtils.isNotEmpty(billsOrder.getBillids())){
                String []ids =  billsOrder.getBillids().split(",");
                ChargeBills a = chargeBillsMapper.get(ids[0]);
             if(ids.length > 1){
              ChargeBills b = chargeBillsMapper.get(ids[ids.length-1]);
              billsOrder.setBillsRemarks(a.getZjCny()+"--"+b.getZjCny()+"("+a.getFeeName()+")");
             }else{
                billsOrder.setBillsRemarks(a.getZjCny()+"("+a.getFeeName()+")");
             }
            }/*else{
                billsOrder.setBillsRemarks(chargeAppService.getRepair(billsOrder.getRepairOrder())+"(维修费)");
            }*/
        }
        //停车费
        List<ChargeBillsOrder>  carOrder = chargeAppService.getPayCarByUid(userId);
        if(carOrder.size() > 0){
            for(ChargeBillsOrder billsOrder:carOrder){
                billsOrder.setBillsRemarks(billsOrder.getPayCny().substring(0,7)+"(停车费)");
            }
            orders.addAll(carOrder);
        }
        //end
         Map<String, List<ChargeBillsOrder>> orderMap = new TreeMap<String, List<ChargeBillsOrder>>(
                 new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                       // 降序排序
                       return obj2.compareTo(obj1);
                    }
                 });
         if(null!=orders && orders.size()>0){
            List<String> cnys=new ArrayList<>();
            for(ChargeBillsOrder o:orders){
               cnys.add(o.getPayCny());
            }
            cnys = removeDuplicate(cnys);
            Collections.sort(cnys, Collections.reverseOrder());
            if(null == cnys || cnys.size() <= 0){
               return Result.ok(orderMap);
            }
           for(String cny:cnys){
              List<ChargeBillsOrder> newOrders=new ArrayList<>();
              for(ChargeBillsOrder o:orders){
                 if(cny.equals(o.getPayCny())){
                    newOrders.add(o);
                 }
                 orderMap.put(cny,newOrders);
              }
           }
         }
         log.info(orderMap.toString());
         return Result.ok(orderMap);
      }catch (Exception e ){
         e.printStackTrace();
         return Result.error("内部异常");
      }
   }

   //去除重复值
   public List<String> removeDuplicate(List<String> list) {
      HashSet<String> h = new HashSet<String> (list);
      list.clear();
      list.addAll(h);
      return list;
   }

   /**
    * 查询物业公司名称
    *
    *
    */
   @AutoLog(value = "查询物业公司名称")
   @ApiOperation(value="查询物业公司名称", notes="查询物业公司名称")
   @PostMapping(value = "/getNameByTenantId")
   public Result<String> getNameByTenantId(String tenantId) {
      String records ="";
      try{
         records=chargeAppService.getNameByTenantId(tenantId);
         return Result.ok(records);
      }catch (Exception e ){
         e.printStackTrace();
         return Result.error("内部异常");
      }
   }



   /**
    * 查询所有项目接口
    *
    *
    */
  /* @AutoLog(value = "查询所有项目接口")
   @ApiOperation(value="查询所有项目接口", notes="查询所有项目接口")
   @PostMapping(value = "/getProjects")
   public Result<List<BaseProject>> getProjects( HttpServletRequest request) {
      List<BaseProject> projects=null;
      try{
         projects=baseRoomService.findProjectAll(request.getHeader("tenantId"));
         return Result.ok(projects);
      }catch (Exception e ){
         e.printStackTrace();
         return Result.error("内部异常");
      }
   }
*/
    /**
     * 查询所有项目接口
     *
     *后续有对应的用户权限下的项目方法时，添加userId做参数再去查询权限的项目即可
     */
    @AutoLog(value = "查询项目下对应的房间")
    @ApiOperation(value="查询项目下对应的房间", notes="查询项目下对应的房间")
    @PostMapping(value = "/getRoomdByPid")
    public Result<List<ChargeAppProperty>> getRoomdByPid() {
       List<ChargeAppProperty> infos=null;
        try{
           infos=chargeAppService.getBaseInfos();
            return Result.ok(infos);
        }catch (Exception e ){
            e.printStackTrace();
            return Result.error("内部异常");
        }
    }
}
